/**
 * form js
 * @author: wdb
 * @date: 2014.11.05
 * @info: 表单模块，负责前端页面的表单逻辑处理
 *
 **/
define(function($, IO, V) {

    var nForm = {
        init: function (conf) {
            this._form = conf.form;
            this.btnSubmit = conf.btnSubmit || this._form.one('.btn-submit');
            this._url = this._form.attr("action");
            this._field = this._form.all('input[name], textarea[name], select[name]');
            this._formData = {};
            this._verifyFiled = this._form.all('input[data-verify], textarea[data-verify]');
            this._callback = conf.sendSuccess || undefined;

            var self = this;
            //失焦验证
            self._verifyFiled.on('blur', function() {
                self.verifyAction(this);
            });

            //提交方法
            self._form.on('submit', function(){
                self.verifyAll();

                if ( self._form.all('input.has-error').length < 1 ) {
                    self.send(self._callback);
                }
                return false;
            });

        },
        //验证展示
        verifyAction: function (that) {

            var self    = this,
                v       = self.validate($(that)),
                parent  = $(that).data('_parent');

            if (v) {

                self.buildVerify(that, v.msg);
                $(that).addClass('has-error');
                $(that).parent().addClass('has-error');
                $(that).parent().parent().addClass('has-error');
            } else if (parent) {

                $(that).removeClass('has-error');
                $(that).parent().removeClass('has-error');
                $(that).parent().parent().removeClass('has-error');
                parent.one('.verify-error').hide();

            } else {

            }
        },
        //建立验证需要的DOM元素
        buildVerify : function(that,msg) {
            var parent = $(that).data('_parent');
            if ( !parent ) {
                $(that).data('_parent', $(that).parent());
                parent = $(that).data('_parent');
                parent.append('<span class="verify-error icon-notification" title="'+msg+'">'+msg+'</span>');
            }
            parent.one('.verify-error').attr('title', msg).html(msg).show();
        },
        //验证逻辑
        validate : function ( node ) {
            var type = node.attr('data-verify').split(' '),
                isOk = undefined,
                temp = {};

            for ( var i = 0, len = type.length; i < len; i++ ) {
                if ( type[i] == 'twice') {
                    temp = V.verify( type[i], node.val(), $('input[name='+node.attr('data-twice')+']').val());
                } else {
                    temp = V.verify(type[i], node.val());
                }
                if ( !temp.ok ) {
                    isOk = temp;
                    break;
                }
            }

            return isOk;

        },
        verifyAll : function() {
            var self = this;
            self._verifyFiled.each(function(item, index) {
                self.verifyAction(item);
            });
        },
        getData : function() {
            var self = this;

            self._field.each(function(item, index) {
                self._formData[$(item).attr('name')] = $(item).val();
            });
        },
        send : function() {

            var self = this;

            self.getData();
           // console.log(self._formData);

            IO.post(self._url, self._formData, function(json){

                if ( json.code == 1 ) {
                    var target = $('input[name='+json.data.field+']');
                    if(target)
                        self.buildVerify(target, json.msg);
                    else
                        alert(json.msg);
                } else if ( json.code == 0 ){
                    if ( self._callback ) {
                        self._callback.call(self, json);
                    }
                }
            }, 'json');
        },
        submit : function() {
            this._form.submit();
        }
    }

    return nForm;

},{requires : ['node','io','dmp-common/validate']});